<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/4
 * Time: 11:05
 */
namespace admin{
    use C\lib\Db;
    use C\lib\Session;
    use C\mod\order;
    use C\mod\worker;
    use C\mod\ads;
    use C\mod\area;
    use C\mod\areaWorker;
    use C\mod\auth;
    use C\mod\workerd;
    use C\lib\Ditu;
    use C\mod\user;
    use C\mod\wprice;
    use C\mod\proute;
    use C\lib\Text;
    class afinance extends ctr{
        /*
         * 财务管理列表
         */
        public function listFinance(){
            if($admin=$this->getLogWorker()){
                $where=[];
                if (isset($_POST['lastId']) && trim($_POST['lastId']) != '') {
                    $lastId = trim($_POST['lastId'] + 0);
                } else {
                    $lastId = 0;
                }
                $lastId = ['id','>',$lastId];
//                if($_POST['lastId'] == 0) {
//                    $lastId = ['id','>',$lastId];
//                } else {
//                    $lastId = ['id','<',$lastId];
//                }
                $where['lastId']=$lastId;
//                if(isset($_POST['order_pid'])){
//                    $map['order_pid']=$_POST['order_pid'];
//                }
                if(isset($_POST['phone'])){
                    $map['phone']=$_POST['phone'];
                }

                $list=[];
                $data=wprice::getList($where,[0,20]);
                if($data){
                    foreach ($data as $val){
                        $map['worker_id']=$val->worker_id;
                        $worker=workerd::get($map);
                        $list[]=[
                            'id'=>$val->id,
                            'worker_id'=>$worker->id,
                            'worker_name'=>$worker->realname,
                            'type'=>$worker->type,
                            'alipay'=>$val->alipay,
                            'wechat'=>$val->wechat,
                            'price'=>$val->price,
                            'price_mortgage'=>$val->price_mortgage,
                            'money'=>$val->money,
                            'pay_state'=>$val->pay_state
                        ];
                    }

                }
                $this->writeJson(1,['list'=>$list]);
            }
        }

        /*
         * 交易记录列表
         */
        public function record(){
            if($admin=$this->getLogWorker()){
                $where=[];
                if (isset($_POST['lastId']) && trim($_POST['lastId']) != '') {
                    $lastId = trim($_POST['lastId'] + 0);
                } else {
                    $lastId = 0;
                }
                $lastId = ['id','>',$lastId];
                $where['lastId']=$lastId;
                if(isset($_POST['phone'])){
                    if(Text::isPhone($_POST['phone'])){
                        $where['phone']=$_POST['phone'];
                    }
                }
                $list=[];
                $data=proute::getList($where,[0,20]);
                if($data){
                    foreach ($data as $val){
                        $worker=workerd::get(['worker_id'=>$val->worker_id]);
                        $list[]=[
                            'id'=>$val->id,
                            'worker_name'=>$worker->realname,
                            'phone'=>$worker->realname,
                            'price'=>$val->price,
                            'remark'=>$val->remark,
                            'pay_type'=>$val->pay_type,
                            'pay_state'=>$val->pay_state,
                            'time_add'=>$val->time_add
                        ];
                    }
                }
                $this->loadView("",['list'=>$list]);
            }
        }


        /*
         * 编辑用户金额表
         */
        public function upWprice(){
            if($admin=$this->getLogWorker()){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'请选择您要编辑的记录!');exit;
                }
                if(empty($_POST['state'])){
                    $this->writeJson(0,'请选择您要进行的操作!');exit;
                }
                $wprice=wprice::get(['id'=>$_POST['id']]);
                $wprice->set([
                    'pay_state'=>$_POST['state']
                ]);
                if($wprice->update()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'操作失败!');
                }
            }
        }



        /*
         * 编辑交易记录
         */
        public function upState(){
            if($admin=$this->getLogWorker()){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'请选择您要编辑的记录!');exit;
                }
                if(empty($_POST['state'])){
                    $this->writeJson(0,'请选择您要进行的操作!');exit;
                }
                $proute=proute::get(['id'=>$_POST['id']]);
                $proute->set([
                    'pay_state'=>$_POST['state']
                ]);
                if($proute->update()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'操作失败!');
                }
            }
        }

    }
}
